$(function(){
    var validateRegExp = {
        tel: "^[0-9\-()（）]{7,18}$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
        mobile: "^0?(13|15|18|14)[0-9]{9}$", //手机
        email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
        num: "^([+-]?)\\d*\\.?\\d+$" //数字
    };
    var regionValidateFlag=true;//默认必须选地区
    var smsPort="identifying_sms.php";
    var ipt=$(".ipt");
    //注册
    $('.submit_btn').click(function(){
        var form=$(this).parents("form");
        var flag=true;
        ipt.each(function(){
            var val = $.trim($(this).val());
            var tip = $(this).data("tip");
            var li = $(this).parents("li");
            var type = $(this).data("type");
            if (val == "") {
                errorMsg("请输入" + tip, li);
                flag = false;
            }else{
                if (type) {
                    switch (type) {
                        case "email":
                            if (!new RegExp(validateRegExp.email).test(val)) {
                                errorMsg("请输入正确的邮箱", li);
                                flag = false;
                            }
                            break;
                        case "mobile":
                            if(!new RegExp(validateRegExp.mobile).test(val)){
                                errorMsg("请输入正确的手机号码", li);
                                flag = false;
                            }
                            break;
                        case "verCode":
                            $.ajax({
                                url:smsPort,
                                data:{"identifying_code":val},
                                async:false,
                                success:function(data){
                                    if(data==""){
                                        errorMsg("验证码错误", li);
                                        flag = false;
                                    }
                                }
                            })

                    }
                }
            }
        });
        if($('.cityHide').val()==""){
            errorMsg("请选择地区", $('.area_wrap'));
            flag = false;
        }
        if(regionValidateFlag){
            if($('.areaHide').val()==""){
                errorMsg("请选择地区", $('.area_wrap'));
                flag = false;
            }
        }
        if(flag){
            form.submit();
        }
    });

    function errorMsg(txt, obj) {
        var msgHtml = '<span class="msg">' + txt + '</span> ';
        obj.find(".msg").remove();
        obj.append(msgHtml)
    }
    ipt.focus(function () {
        $(this).parents("li").find(".msg").remove();
    });


    //省市区
    function Area() {
        this.pv = $('#province');
        this.cy = $('#city');
        this.ar = $('#area');
        this.pvLabel = $('.province_label');
        this.cyLabel = $('.city_label');
        this.arLabel = $('.area_label');
        this.data = areaDate;
        var t = this;
        this.pv.change(function () {
            var val = $(this).val();
            var PvIdx = $(this).find("option:selected").data("idx");
            var id=$(this).find("option:selected").data("id");
            t.pvLabel.val(val);
            $('.proHide').val(id);
            t.cy.data("PvIdx", PvIdx);
            t.cyLabel.val("市").data("PvIdx", PvIdx);
            t.arLabel.val("区");
            t.getCity(PvIdx);
            $('.area_wrap').find(".msg").remove();
        });
        this.cy.change(function () {
            var val = $(this).val();
            var PvIdx = $(this).data("PvIdx");
            var cyIdx = $(this).find("option:selected").data("idx");
            var id=$(this).find("option:selected").data("id");
            $('.cityHide').val(id);
            t.cyLabel.val(val);
            t.arLabel.val("区");
            t.getArea(PvIdx, cyIdx);
            $('.area_wrap').find(".msg").remove();

        });
        this.ar.change(function () {
            var val = $(this).val();
            var id=$(this).find("option:selected").data("id");
            $('.areaHide').val(id);
            t.arLabel.val(val);
            $('.area_wrap').find(".msg").remove();

        });
        this.createPro();
    }

    Area.prototype = {
        createPro: function () {
            var opt = "<option value='省'>省</option>";
            for (var i in this.data) {
                opt += '<option class="pv" data-id="' + this.data[i]["1"] + '" data-idx="' + i + '">' + this.data[i]["0"] + '</option>';
            }
            this.pv.append(opt)
        },
        getCity: function (PvIdx) {
            var city = this.data[PvIdx].city;
            var opt = "<option value='市'>市</option>";
            this.ar.empty().append(opt);
            $(".cityHide").val("");
            $(".areaHide").val("");
            for (var i in city) {
                opt += '<option class="cy" data-id="' + city[i]["1"] + '" data-idx="' + i + '">' + city[i]["0"] + '</option>';

            }
            this.cy.empty().append(opt);
        },
        getArea: function (p, c) {
            var area = this.data[p].city[c].district;
            var opt = "<option value='区'>区</option>";
            $(".areaHide").val("");
            if (!area.length) {
                regionValidateFlag=false;
                $(".area_wrap").hide();
            } else {
                regionValidateFlag=true;
                $(".area_wrap").show();
                for (var i in area) {
                    opt += '<option class="cy" data-id="' + area[i]["1"] + '" data-idx="' + i + '">' + area[i]["0"] + '</option>';
                }
                this.ar.empty().append(opt);
            }

        }
    };

    if(typeof(areaDate)!="undefined"){var area = new Area();}


    //获奖名单
    $(".other_list li").each(function(){
        var i=$(this).index();
        if(i%2!=0){
            $(this).addClass("odd")
        }
    });


    //头部导航
    function showNavigation() {
        var $handle = $('.topnavhandle'),
            $handleArr = $handle.find('span'),
            $up = $('#navigation-up'),
            $menu = $('#navigation'),
            $menuShade = $('#menuShade');
        if ($menuShade.size() == 0) {
            $menuShade = $('<div id="menuShade"></div>').appendTo('body');
        }

        function _menuUnfold() {
            $menu.animate({height:323}, 500,'ease-out');
            $handleArr.addClass('on');
            $menuShade.show().css("opacity",.6);
        }

        function _menuFold() {
            $menu.animate({height:0}, 500,'ease-out',function(){ $menuShade.hide();});
            $handleArr.removeClass('on');
        }
        $handle.click(function() {
            $handleArr.hasClass('on') ? _menuFold() : _menuUnfold();
        });
        $up.click(function() {
            _menuFold();
        })
    }
    showNavigation();
});